var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cars:[],
    isShouquan:'yes',
    goType:'',
    is_overseas: 0, 
    allPrice:0,//商品总额
    payPrice:0,//实付金额
    isReady:true,
    remark:'',
    submitting:false,
    sku_id:0,
    singleSku:[],
    singleCount:0,
    singlePrice:0,
    order_type:0,
    count:0,
    is_has_address:0,
    address:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options',options)
    var that = this
    var goType = options.goType
    that.setData({ goType: goType })

    that.data.sku_id = parseInt(options.sku_id)
    that.data.count = parseInt(options.count)

    if (goType == 'buynow') {
      that.onHttpInitBuyNow(options)
    } else {
      that.onHttpInitByCart()
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (t) {
    console.log('onShow',t)
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInitByCart: function () {
    var that = this
    fetchApi.httpPost('/product/cart/previewList', {
     }, 
      function (res) {
        that.setData({ isReady: true, isShouquan: 'yes' })
        console.log('订单结算数据', res)
        if (res.data.code == 0) {
          that.data.cars = res.data.data.cars
          that.setData({
            cars: res.data.data.cars,
            allPrice: res.data.data.allPrice,//实付金额
            payPrice: res.data.data.payPrice,//实付金额
            is_has_address: res.data.data.is_has_address,
            address:res.data.data.is_has_address == 1 ? res.data.data.address:{},
          })
          // that.getAllProductCount(res.data.data.cars)
          wx.stopPullDownRefresh() //停止下拉刷新
        } else {
          console.log("订单结算数据 异常", res.data.msg)
        }
    },function(){})
  },
  /**
   * 立即购买的跳转加载
   */
  onHttpInitBuyNow:function(options){
    var that = this
    fetchApi.httpPost('/shoppingcar/buynow', {
      sku_id: parseInt(options.sku_id),
      count: parseInt(options.count),
    }, function (res) {
      if (res.data.code == 0) {
        that.setData({ hasCard: true, isShowSkuPop: false })
        that.data.singleSku = res.data.data.sku
        that.data.singleCount = res.data.data.count
        that.setData({
          allPrice: res.data.data.totalPrice,//实付金额
          payPrice: res.data.data.payPrice,//实付金额
          is_has_address: res.data.data.is_has_address,
          address:res.data.data.is_has_address == 1 ? res.data.data.address:{},
        })
        that.getBuynowProductCount(res.data.data.sku, res.data.data.count)
        wx.stopPullDownRefresh() //停止下拉刷新
      } else {
        console.log("添加购物车 异常", res.data.msg)
      }
    },function(){})
  },
  //立即购买的数据处理
  getBuynowProductCount: function (sku,count) {
    var that = this
    var price = sku.price * count
    that.data.allPrice = price;
    console.log('price',price)
    that.setData({
      allPrice: price,
      singleCount: count,
      payPrice: price,
    })
  },
  //从购物车过来的
  getAllProductCount: function (cars) {
    var that = this
    var count = 0;
    var price = 0.00
    that.data.checkAll = 1
    for (var index in cars) {
      if (cars[index].checkState == 1) {
        count += cars[index].cartNum
        price += cars[index].attrValue.price * cars[index].cartNum
      } else {
        that.data.checkAll = 0
      }
      cars[index].totalPrice = cars[index].attrValue.price * cars[index].cartNum
    }
    that.data.allPrice = price
    that.setData({
      cars: cars,
      allPrice: price,
      payPrice: price,
    })
  },
  pushToAddress:function(){
    wx.navigateTo({
      url: "/pages/address/index?type=1"
    })
  },
  msgHandle:function(a){
    this.setData({ remark: a.detail.value });
  },
  submitOrder:function(e){

    if(this.data.address.id==undefined ){
      Toast.fail("请选择所在地址");
      return ;
    }

    var that = this
    that.setData({ submitting : true})
    fetchApi.httpPost('/product/order/addOrder' + (that.data.goType == 'buynow' ? 'BuyNow' : 'ByCart'), {
      address_id: that.data.address.id,
      order_type: that.data.order_type,
      remark: that.data.remark,
    }, function (res) {
      console.log('res', res)
      if (res.data.code == 0) {
        var payNo = res.data.data;
        that.requestPay(payNo)
        console.log('payNo',payNo)
      } else {
        Toast.fail(res.data.msg);
      }
    },function(){})
  },
  requestPay:function(payNo){
    var that = this
    fetchApi.httpPost('/pay/payByPayNo', {
      pay_no: payNo,
      subject:'购买线上商品/服务'
    }, function (res) {
      console.log('提交订单',res)
      if (res.data.code == 0) {
        var pay_data = res.data.data;
        var wxpayinfo = JSON.parse(res.data.data)
        console.log('pay_data',pay_data)
        if(wxpayinfo.result_code == 'SUCCESS'){
          wx.requestPayment({
            timeStamp: wxpayinfo.timeStamp,
            nonceStr: wxpayinfo.nonceStr,
            package: wxpayinfo.package,
            signType: wxpayinfo.signType,
            paySign: wxpayinfo.paySign,
            success (res) { 
              console.log('requestPayment success',res)
              wx.redirectTo({
                url: '/pages/payResult/payresult?order_id=' + order_id,
              })
            },
            fail (res) {
              console.log('requestPayment fail',res)
             }
          })
        }
      } else {
        that.setData({ isLoading: false, loading: true, netWork: true })
      }
    }, function () {
        that.setData({  isLoading: false,  loading: true,  netWork: true, }) 
    })
  },
})